<?php

namespace app\controller\admin\ec;

use think\Request;
use \think\facade\Validate;
use app\eslib\EsUtils;
use app\model\ec\CommentsModel;

/**
 * 商品评论 控制器
 * @version 1.0.0
 */
class EcComments
{
    // 控制器中间件
    protected $middleware = [\app\middleware\JwtAuthForAdmin::class];
    
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        // 特殊模式的列表查询
        $model = $request->param('_mode', '');
        switch ($model) {
            case 'dict':
                // 查询 KV 数据（常用于表单或者搜索框）
                return json(EsUtils::stdMessage(true, '', CommentssModel::getDictData()));
            default:
                break;
        }
        
        // 查询常规的列表数据
        $searchText = $request->param('search_text', '');
        $page = intval($request->param('page', 1));
        $pageSize = intval($request->param('page_size', 10));
        $data=CommentssModel::getListData($page, $pageSize, $searchText);
        return json(EsUtils::stdMessage(true, '', $data));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $formData = $request->all();
        $validator = Validate::rule([
            'pid|父级ID' => 'require|number',
'user_id|用户ID' => 'require|number',
'goods_id|商品ID' => 'require|number',
'order_id|订单ID' => 'require|number',
'rating|评分' => 'require|number',
'product_score|商品分数' => 'require|number',
'service_score|服务分数' => 'require|number',
'content|评论内容' => 'require|max:500',
'images|图片' => 'max:65535',
'is_anonymous|是否匿名' => 'require|number',
'status|状态' => 'require|number',
        ]);
        if (!$validator->check($formData)) {
        return json(EsUtils::stdMessage(false, $validator->getError()));
}
        // 表单数据处理
        // ...
        try {
            $model = CommentssModel::create($formData);
            return json(EsUtils::stdMessage(true, '', $model->id));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if (!is_numeric($id)) {
            return json(EsUtils::stdMessage(false, 'id值无效', $id));
        }
        if ($id > 0) {
            $model = CommentssModel::find($id);
            if (!$model) {
                return json(EsUtils::stdMessage(false, '记录不存在'));
            }
        } else {
            $model = new CommentssModel();
            $model->is_anonymous = 0;
$model->status = 0;
$model->pid = 0;
$model->rating = 0;
$model->product_score = 0;
$model->service_score = 0;
$model->is_anonymous = 0;
$model->status = 0;
        }
        $data = [
            'data' => $model,
            'pid_data' => CommentssModel::getDictData(),
            'is_anonymous_data' => $model->getIsAnonymousDataAttr(),
'status_data' => $model->getStatusDataAttr(),
        ];
        return json(EsUtils::stdMessage(true, $id, $data));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $formData = $request->all();
        $validator = Validate::rule([
            'pid|父级ID' => 'require|number',
'user_id|用户ID' => 'require|number',
'goods_id|商品ID' => 'require|number',
'order_id|订单ID' => 'require|number',
'rating|评分' => 'require|number',
'product_score|商品分数' => 'require|number',
'service_score|服务分数' => 'require|number',
'content|评论内容' => 'require|max:500',
'images|图片' => 'max:65535',
'is_anonymous|是否匿名' => 'require|number',
'status|状态' => 'require|number',
        ]);
        if (!$validator->check($formData)) {
        return json(EsUtils::stdMessage(false, $validator->getError()));
}
        try {
            $affected = CommentssModel::where('id', $id)->update($formData);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            $affected = CommentssModel::destroy($id);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 复制指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function copy($id)
    {
        try {
            $affected = CommentssModel::copy($id);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }
}
